﻿/*
 * 原作者: blu10ph
 * 
 */
using System.Collections.Generic;
using System.Text;
using System;

namespace cn.blu10ph.wp.HttpHelper
{
    public static class HttpGetHelper
    {
        public static Dictionary<string, string> GetParamtersFromQueryString(string QueryString, char spliter = '&')
        {
            Dictionary<string, string> parameters = new Dictionary<string, string>();
            string[] parametersString = QueryString.Split(spliter);
            foreach (string parameterString in parametersString)
            {
                string[] parameter = parameterString.Split('=');
                if (parameter.Length > 2)
                {
                    parameters.Add(parameter[0], parameter[1]);
                }
                else if (parameter.Length > 1)
                {
                    parameters.Add(parameter[0], "");
                }
            }
            return parameters;
        }
        public static string GetQueryStringByParameters(Dictionary<string, object> parameters)
        {
            return GetQueryStringByParameters(parameters, '&');
        }
        public static string GetQueryStringByParameters(Dictionary<string, object> parameters, char spliter)
        {
            StringBuilder buffer = new StringBuilder();
            bool notFirst = false;
            foreach (string key in parameters.Keys)
            {
                if (notFirst)
                {
                    buffer.AppendFormat("{0}{1}={2}", spliter, key, parameters[key]);
                }
                else
                {
                    buffer.AppendFormat("{0}={1}", key, parameters[key]);
                    notFirst = true;
                }
            }
            return buffer.ToString();
        }
        public static string GetQueryStringByParameters(Dictionary<string, object> parameters, string uriPrefix)
        {
            return GetQueryStringByParameters(parameters, uriPrefix, '&');
        }
        public static string GetQueryStringByParameters(Dictionary<string, object> parameters, string uriPrefix, char spliter)
        {
            StringBuilder buffer = new StringBuilder();
            string parametersString = GetQueryStringByParameters(parameters, spliter);
            buffer.AppendFormat("{0}?{1}", uriPrefix, parametersString);
            return buffer.ToString();
        }
    }
}
